/**
 * 
 */
define(["jquery", "knockout","durandal/composition",'BMap','panel',
         "http", "i18nCommon","kovholder",'WdatePicker','css!style/common/common-style'],
		function($, ko, composition, baiduMap, panal, http, i18nCommon,kov,WdatePicker){

	function model(){
    	var self = this;
    	 
    	var p,_dom,_human,detailInfo,map,points;
    	var index = 0;
    	var timer; //定时器
    	var car; //汽车图标
    	var label; //信息标签
    	
    	self.playDate = ko.observable();
    	self.startTime = ko.observable();
    	self.endTime = ko.observable();

		composition.addBindingHandler("traceplay-init",{
			init: function(dom){
				p = panal.getPanalByElement(dom);
				_dom = dom;
				map = p.param.map;
				initUI();
				initData();
				p.settings.onClose = function () {
					map.clearOverlays();
				}
		}});
		
		function initUI() {
			$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stop.png");
			$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/play.png");
			$(_dom).find('.detailinfo').click(function(){
				panal.getInstance("view/mis/patrol/patrolinfo", {
					width : 1000,
					height : 600
				}, {
					patrolId : detailInfo.patrol.patrolId
				});

			});

			 $(".chooseDate").click(function () {
                 WdatePicker({
                     readOnly: false,
                     dateFmt: 'yyyy-MM-dd',
                     onpicking: function (dp) {
                    	 self.playDate(dp.cal.getNewDateStr());
                     }
                 });
             });
			 
			$(_dom).find('.playtrace').click(function(){ 				
				play();
			});
			
			$(_dom).find('.playBtn').mousedown(function(){ 				
				if($(_dom).find('.playBtn')[0].src === cywork.global.rootPath + "style/common/images/play.png"){
					$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/playclick.png");
				}else{
					$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/playingclick.png");
				}
			});
			
			$(_dom).find('.stopBtn').mousedown(function(){ 				
				if($(_dom).find('.stopBtn')[0].src === cywork.global.rootPath + "style/common/images/stop.png"){
					$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stopclick.png");
				}else{
					$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stopingclick.png");
				}
			});
			
			$(_dom).find('.playBtn').mouseup(function(){ 				
				if($(_dom).find('.playBtn')[0].src === cywork.global.rootPath + "style/common/images/playclick.png"){
					$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/playing.png");
					$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stop.png");
				}else{
					$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/play.png");
				}
			});
			
			$(_dom).find('.stopBtn').mouseup(function(){ 				
				if($(_dom).find('.stopBtn')[0].src === cywork.global.rootPath + "style/common/images/stopclick.png"){
					$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stoping.png");
					$(_dom).find('.playBtn').attr('src',cywork.global.rootPath + "style/common/images/play.png");
				}else{
					$(_dom).find('.stopBtn').attr('src',cywork.global.rootPath + "style/common/images/stop.png");
				}
			});
		}
		
		function play(){
			var point = points[index];
			if(index > 0) {
				map.addOverlay(new BMap.Polyline([points[index - 1], point], {strokeColor: "red", strokeWeight: 1, strokeOpacity: 1}));
			}
			label.setContent("经度: " + point.lng + "<br>纬度: " + point.lat);
			car.setPosition(point);
			index++;
//			if(followChk.checked) {
//				map.panTo(point);
//			}
			if(index < points.length) {
				timer = window.setTimeout(play, 200);
			}else{
				map.panTo(point);
			}
		}
		
		function initData() {

			map.clearOverlays();
			var curDate = new Date();
			self.playDate(curDate.getFullYear()+"-"+curDate.getMonth()+"-"+curDate.getDate());
			$(_dom).find('.startTime')[0].options[8].selected = true;
			$(_dom).find('.endTime')[0].options[21].selected = true;
			points = [
			    new BMap.Point(121.580042,29.842591), new BMap.Point(121.579467,29.842027),
				new BMap.Point(121.578892,29.841088), new BMap.Point(121.578892,29.841088),
				new BMap.Point(121.578102,29.839709), new BMap.Point(121.578102,29.839709),
				new BMap.Point(121.579251,29.839145), new BMap.Point(121.579898,29.838832),
				new BMap.Point(121.580473,29.838519), new BMap.Point(121.581192,29.838205),
				new BMap.Point(121.581479,29.838143), new BMap.Point(121.582701,29.838331),
				new BMap.Point(121.583132,29.843891),new BMap.Point(121.585288,29.84245),
				new BMap.Point(121.588737,29.840069),new BMap.Point(121.585791,29.835871),
				new BMap.Point(121.58342,29.831861)
				];
			map.centerAndZoom(points[0], 15);
							 
			//通过DrivingRoute获取一条路线的point
			var driving = new BMap.DrivingRoute(map);
			driving.search(points[0], points[points.length-1]);
			driving.setSearchCompleteCallback(function() {
				//得到路线上的所有point
				points = driving.getResults().getPlan(0).getRoute(0).getPath();
				//画面移动到起点和终点的中间
				var centerPoint = new BMap.Point((points[0].lng + points[points.length - 1].lng) / 2, (points[0].lat + points[points.length - 1].lat) / 2);
				map.panTo(centerPoint);
				//连接所有点
				map.addOverlay(new BMap.Polyline(points, {strokeColor: "blue", strokeWeight: 5, strokeOpacity: 1}));
				
				//显示执法队员图标
				label = new BMap.Label("", {offset: new BMap.Size(25, -5)});
				label.setStyle({
					 color : "black",
					 fontSize : "12px",
					 height : "20px",
					 lineHeight : "20px",
					 backgroundColor :"0.05",
					 border :"0",
					 fontFamily:"微软雅黑"
				 });
				var myIcon = new BMap.Icon(cywork.global.rootPath + "style/common/images/lawhuman.png", new BMap.Size(20,40));
				car = new BMap.Marker(points[0],{icon:myIcon});
				car.setLabel(label);
				map.addOverlay(car);
			});
		}

		function renderUI(data) {
			detailInfo = data;
			var law = data.law || {},
			    human = data.human || {};
			data.img = data.img || eUrban.global.rootPath + "style/urban/common/css/images/img-default.png";
			$(_dom).find('.name').text(patrol.patrolName);
			$(_dom).find('.region').text(data.region);
			$(_dom).find('.unit').text(human.unitName);
		    $(_dom).find('.screen-patrol-photo').attr('src',data.img);
		}
		
		self.playTrace = function () {
			play();
        };
        
        self.stop = function(){
        	if(timer){
        		clearTimeout(timer);
        	}       	
        }
 
	}
	
    return model;
});